﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Smart.CaloriesCalculatorService;

namespace Smart.Pages
{
    public partial class Nutrition :Smart.UI.BaseClasses.AuthenticatedPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {  
                BindgvMeal();
                gvMeals.DataBind();
            }
        }
        protected void BindgvMeal()
        {   
            List<MealTiny> list = Global.CaloriesCalculatorService.GetAllMeals();
            gvMeals.DataSource = list;  
        }
    

         protected void gvMeals_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            BindgvMeal();
            gvMeals.PageIndex = e.NewPageIndex;
            gvMeals.DataBind();
        }

         protected void Button3_Click(object sender, EventArgs e)
         {
             int mealId;
             int quantity;

             try
             {
                 mealId = Int32.Parse(tbMealId.Text);
                 quantity = Int32.Parse(tbQuantity.Text);

                 if (mealId > 0 && quantity >= 0)
                 {
                     Global.CaloriesCalculatorService.RecordMeal(CurrentUserId, mealId, quantity);
                     lblResult.Text = "Успешно въведен запис!";
                 }
                 else
                     throw new Exception();  
             }
             catch (Exception)
             {
                 lblResult.Text = "Невалидни стойности за ядене или количество!";                 
             }
         }

         protected void Button2_Click(object sender, EventArgs e)
         {
             int mealId;
             int quantity;
             
             try
             {
                 mealId = Int32.Parse(tbMealId.Text);
                 quantity = Int32.Parse(tbQuantity.Text);

                 if (mealId > 0 && quantity >= 0)
                 {
                     CalculatedResult res = Global.CaloriesCalculatorService.CalculateCaloriesForMeal(mealId,quantity);
                     lblResult.Text = "Хранителната стойност е: " + res.TotalCals + " Калории, " + res.TotalCarbs + " Въглехидрати, "
                         + res.TotalFats + " Мазнини, " + res.TotalProts + " Протеини.";
                 }
                 else
                     throw new Exception();  
             }
             catch (Exception)
             {
                 lblResult.Text = "Невалидни стойности за ядене или количество!";                 
             }             
         }

     

   
    }
}